package com.jumaojiang.exceptions;

import com.alibaba.fastjson.JSON;
import com.jumaojiang.vo.AjaxResultVo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 全局异常处理
 * @author wuhanwei
 * @version 1.0
 * @date 2021/10/28
 */
@Component
public class GlobalExceptionHandle implements HandlerExceptionResolver {
    // 日志输出
    private final Logger logger = Logger.getLogger(org.apache.commons.logging.Log.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        ModelAndView mv = new ModelAndView();
        AjaxResultVo resultVo = null;
        // JWTToken验证失败异常
        if(e instanceof JWTTokenInvalidException){
            String message = e.getMessage();
            logger.debug(message);
            if("token失效".equals(message)){
                resultVo = new AjaxResultVo(403, "登录信息过期,请重新登陆!");
            }else if("没有token".equals(message)) {
                resultVo = new AjaxResultVo(401, "未登录");
            }
//            // 判断请求的如果是ajax
//            if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
//                if("token失效".equals(message)){
//                    response.setHeader("TOKEN-MSG", "token-invalid");
//                }else if("没有token".equals(message)){
//                    response.setHeader("TOKEN-MSG", "no-token");
//                }
//                // 返回重定向head标志给前端ajax
//                response.setHeader("REDIRECT", "REDIRECT");
//                // 返回重定向路径给前端ajax
//                response.setHeader("CONTENTPATH", "/index/login.html");
//                response.setStatus(HttpServletResponse.SC_FORBIDDEN); // 添加403状态码 (服务器拒绝)
//            }else{
//                try {
//                    response.sendRedirect("/index/login.html");
//                } catch (IOException ex) {
//                    ex.printStackTrace();
//                }
//            }
        }

        // Resource处理异常
        else if(e instanceof ResourceException){
            String message = e.getMessage();
            logger.debug(message);
            if("今日次数已用光".equals(message)){
                resultVo = new AjaxResultVo(600, "今日次数已用光");
            }else if("每日可获取次数至少保留1次".equals(message)){
                resultVo = new AjaxResultVo(602, "每日可获取次数至少保留1次");
            }else if("视频重复".equals(message)){
                resultVo = new AjaxResultVo(603, "视频重复");
            }else{
                resultVo = new AjaxResultVo(603, message);
            }
        }

        else{
            logger.debug(e.getMessage());
            resultVo = new AjaxResultVo(500, e.getMessage());
        }

        if(resultVo != null){
            String s = JSON.toJSON(resultVo).toString();
            response.setContentType("application/json;charset=utf-8");
            PrintWriter writer = null;
            try {
                writer = response.getWriter();
                writer.write(s);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            writer.flush();
            writer.close();
        }

        return mv;
    }
}
